"""
https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array/

"""


class Solution:

    def searchRange(self, nums,target):
        min = 0
        max = len(nums)-1
        mid_index = -1
        index = (min + max) // 2

        ##找到是否存在
        while (min<=index and max>=index):
            if nums[index]>target:
                max = index-1
                index = (min + max) // 2
            elif nums[index]<target:
                min = index+1
                index = (min + max) // 2
            else:
                mid_index = index
                break
        if mid_index == -1:
            return [-1,-1]

        #开始back_index往后找，font_index往前找
        back_index = mid_index-1
        font_index = mid_index+1
        while (back_index >= 0 and nums[back_index] == nums[mid_index]):
            back_index = back_index-1

        while (font_index < len(nums) and nums[font_index] == nums[mid_index]):
            font_index = font_index+1

        return [back_index+1,font_index-1]


if __name__ == '__main__':
    s = Solution()
    print (s.searchRange([1,1,1,7,6,10],10))

